
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.JComboBox;

public class Main extends JFrame{

    int DEUAULT_WIDTH = 700;
    int DEUAULT_HEIGHT = 1000;

    private List<Postcode> postcodeList = new ArrayList<Postcode>();

    private JLabel label=new JLabel("Load csv file: ");
    private JTextField jtf=new JTextField(30);
    private JButton button=new JButton("browse");
    private JButton loadBtn = new JButton("Start");
    private JButton loadCancelBtn = new JButton("Cancel");

    private JLabel queryLabel=new JLabel("Query by: ");
    private JRadioButton radioButton1=new JRadioButton("distance to a station",true);
    private JRadioButton radioButton2=new JRadioButton("terminate date");

    private JLabel queryStation=new JLabel("Choose a station:  ");
    private JLabel queryDate=new JLabel("Choose a date:  ");
    private JComboBox stationCmb = new JComboBox();
    private JComboBox dateCmb = new JComboBox();
    private JButton queryBtn = new JButton("Query");


    private JLabel postcodeLabel = new JLabel("Postcode: ");
    private JTextField postcodeJtf = new JTextField(10);
    private JLabel districtLabel = new JLabel("District: ");
    private JComboBox districtCmb = new JComboBox();
    private JLabel constituencyLabel = new JLabel("Constituency: ");
    private JComboBox constituencyCmb = new JComboBox();

    private PostcodeTable postcodeTable = null ;

    private JLabel recordLabel = new JLabel("");

    private JButton add = new JButton("Add");
    private JButton save = new JButton("Save");
    private JButton next = new JButton("Next");
    private JButton previous = new JButton("previous");



    public Main()
    {
        initTableData();

        setTitle("GridBagLayoutTest");
        setSize(DEUAULT_WIDTH, DEUAULT_HEIGHT);
        GridBagLayout layout = new GridBagLayout();
        setLayout(layout);


        add(label, new GBC(0, 0).setAnchor(GBC.EAST));
        add(jtf, new GBC(1, 0,4,1).setFill(GBC.BOTH).setAnchor(GBC.WEST).setWeight(100, 0));
        add(button, new GBC(5, 0).setAnchor(GBC.WEST).setWeight(100, 0));
        add(loadBtn, new GBC(4,1).setFill(GBC.VERTICAL).setAnchor(GBC.EAST).setWeight(100, 0));
        add(loadCancelBtn, new GBC(5, 1).setAnchor(GBC.WEST).setWeight(100, 0));

        ButtonGroup group=new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        add(queryLabel, new GBC(0, 2).setFill(GBC.VERTICAL).setAnchor(GBC.EAST).setWeight(100, 0)
                .setInsets(10));
        add(radioButton1, new GBC(1, 2,2,1).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(10));
        add(radioButton2, new GBC(3, 2,2,1).setFill(GBC.VERTICAL).setAnchor(GBC.CENTER).setWeight(100, 0)
                .setInsets(10));


        add(queryStation, new GBC(1, 3,2,1).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(5));
        add(stationCmb, new GBC(3, 3,2,1).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(5));
        add(queryBtn, new GBC(5, 3,1,1).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(5));

//        JPanel jp5 = new JPanel();
//        jp5.setLayout(new FlowLayout());
//        jp5.add(postcodeLabel);
//        jp5.add(postcodeJtf);
//        jp5.add(districtLabel);
//        jp5.add(districtCmb);
//        jp5.add(constituencyLabel);
//        jp5.add(constituencyCmb);

        add(postcodeLabel, new GBC(0, 4).setFill(GBC.VERTICAL).setAnchor(GBC.EAST).setWeight(100, 0)
                .setInsets(10));
        add(postcodeJtf, new GBC(1, 4).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(10));
        add(districtLabel, new GBC(2, 4).setFill(GBC.VERTICAL).setAnchor(GBC.EAST).setWeight(100, 0)
                .setInsets(10));
        add(districtCmb, new GBC(3, 4).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(10));
        add(constituencyLabel, new GBC(4, 4).setFill(GBC.VERTICAL).setAnchor(GBC.EAST).setWeight(100, 0)
                .setInsets(10));
        add(constituencyCmb, new GBC(5, 4).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(10));

        postcodeTable = new PostcodeTable(postcodeList);
        JScrollPane jp6 = new JScrollPane(postcodeTable);
        add(jp6, new GBC(0, 5,6,1).setAnchor(GBC.CENTER).setFill(GBC.BOTH));

//        JPanel jp7 = new JPanel();
//        jp7.setLayout(new FlowLayout());
//        jp7.add(recordLabel);
//
//        JPanel jp8 = new JPanel();
//        jp8.setLayout(new FlowLayout());
//        jp8.add(add);
//        jp8.add(save);
//        jp8.add(previous);
//        jp8.add(next);
//
        recordLabel = new JLabel("总共" + postcodeTable.totalRowCount + "记录|当前第"
                + postcodeTable.currentPage + "页");
        add(recordLabel, new GBC(0, 7).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(10));

        add(add, new GBC(0, 8).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(10));
        add(save, new GBC(1, 8).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(10));
        add(previous, new GBC(4, 8).setFill(GBC.VERTICAL).setAnchor(GBC.EAST).setWeight(100, 0)
                .setInsets(10));
        add(next, new GBC(5, 8).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(100, 0)
                .setInsets(10));


//        add(italic,
//                new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
//        add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100));



//
//        JPanel jp5 = new JPanel();
//        jp5.setLayout(new FlowLayout());
//        jp5.add(postcodeLabel);
//        jp5.add(postcodeJtf);
//        jp5.add(districtLabel);
//        jp5.add(districtCmb);
//        jp5.add(constituencyLabel);
//        jp5.add(constituencyCmb);
//
//
//
//        postcodeTable = new PostcodeTable(postcodeList);
//        JScrollPane jp6 = new JScrollPane(postcodeTable);
//        jp4.setBounds(10, 40, 600, 400);
//
//        JPanel jp7 = new JPanel();
//        jp7.setLayout(new FlowLayout());
//        jp7.add(recordLabel);
//
//        JPanel jp8 = new JPanel();
//        jp8.setLayout(new FlowLayout());
//        jp8.add(add);
//        jp8.add(save);
//        jp8.add(previous);
//        jp8.add(next);
//
////        previous = new JButton("上一页");
////        previous.setBounds(150, 150, 75, 20);
////        next = new JButton("下一页");
////        next.setBounds(255, 150, 75, 20);
////        add = new JButton("添加");
////        add.setBounds(350, 150, 65, 20);
////        delete = new JButton("删除");
////        delete.setBounds(420, 150, 65, 20);
//
//
//
////        previous.addActionListener(this);
////        next.addActionListener(this);
////        add.addActionListener(this);
////        delete.addActionListener(this);
////        label1 = new JLabel("总共" + postcodeTable.totalRowCount + "记录|当前第"
////                + postcodeTable.currentPage + "页");
////        label1.setBounds(10, 150, 130, 20);
//
////        jf.add(previous);
////        jf.add(next);
////        jf.add(add);
////        jf.add(delete);
////        jf.add(label1);
////        jf.setLocationRelativeTo(null);
//
//
//        jf.add(jp1);
//        jf.add(jp2);
//        jf.add(jp3);
//        jf.add(jp4);
//        jf.add(jp5);
//        jf.add(jp6);
//        jf.add(jp7);
//        jf.add(jp8);
//
//
////        jf.pack();    //自动调整大小
//        jf.setVisible(true);
//        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        button.addActionListener(new MyActionListener());    //监听按钮事件
    }

//    public Main()
//    {
//        initTableData();
//
//        JFrame jf=new JFrame("London Postcode");
//
//        jf.setLayout(new GridLayout(8,1,0,0));
//
//        jf.setSize(700,1000);
//
//        JPanel jp1=new JPanel();
//        jp1.setLayout(new FlowLayout());
//
//        jp1.add(label);
//        jp1.add(jtf);
//        jp1.add(button);
//        jp1.setBackground(Color.BLUE);
//
//
//        JPanel jp2=new JPanel();
//        jp2.setLayout(new FlowLayout());
////        loadBtn.setBounds(600,20,40,20);
////        loadCancelBtn.setBounds(700,20,40,20);
//        jp2.add(loadBtn);
//        jp2.add(loadCancelBtn);
//        jp2.setBackground(Color.YELLOW);
//
//        JPanel jp3 = new JPanel();
//        jp3.setLayout(new FlowLayout());
////        queryLabel.setBounds(10,30,40,20);
////        radioButton1.setBounds(100,30,50,20);
////        radioButton2.setBounds(200,30,50,20);
//        ButtonGroup group=new ButtonGroup();
//        group.add(radioButton1);
//        group.add(radioButton2);
//        jp3.add(queryLabel);
//        jp3.add(radioButton1);
//        jp3.add(radioButton2);
//
//        JPanel jp4 = new JPanel();
//        jp4.setLayout(new FlowLayout());
//        jp4.add(queryStation);
//        jp4.add(stationCmb);
//        jp4.add(queryBtn);
//
//        JPanel jp5 = new JPanel();
//        jp5.setLayout(new FlowLayout());
//        jp5.add(postcodeLabel);
//        jp5.add(postcodeJtf);
//        jp5.add(districtLabel);
//        jp5.add(districtCmb);
//        jp5.add(constituencyLabel);
//        jp5.add(constituencyCmb);
//
//
//
//        postcodeTable = new PostcodeTable(postcodeList);
//        JScrollPane jp6 = new JScrollPane(postcodeTable);
//        jp4.setBounds(10, 40, 600, 400);
//
//        JPanel jp7 = new JPanel();
//        jp7.setLayout(new FlowLayout());
//        jp7.add(recordLabel);
//
//        JPanel jp8 = new JPanel();
//        jp8.setLayout(new FlowLayout());
//        jp8.add(add);
//        jp8.add(save);
//        jp8.add(previous);
//        jp8.add(next);
//
////        previous = new JButton("上一页");
////        previous.setBounds(150, 150, 75, 20);
////        next = new JButton("下一页");
////        next.setBounds(255, 150, 75, 20);
////        add = new JButton("添加");
////        add.setBounds(350, 150, 65, 20);
////        delete = new JButton("删除");
////        delete.setBounds(420, 150, 65, 20);
//
//
//
////        previous.addActionListener(this);
////        next.addActionListener(this);
////        add.addActionListener(this);
////        delete.addActionListener(this);
////        label1 = new JLabel("总共" + postcodeTable.totalRowCount + "记录|当前第"
////                + postcodeTable.currentPage + "页");
////        label1.setBounds(10, 150, 130, 20);
//
////        jf.add(previous);
////        jf.add(next);
////        jf.add(add);
////        jf.add(delete);
////        jf.add(label1);
////        jf.setLocationRelativeTo(null);
//
//
//        jf.add(jp1);
//        jf.add(jp2);
//        jf.add(jp3);
//        jf.add(jp4);
//        jf.add(jp5);
//        jf.add(jp6);
//        jf.add(jp7);
//        jf.add(jp8);
//
//
////        jf.pack();    //自动调整大小
//        jf.setVisible(true);
//        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        button.addActionListener(new MyActionListener());    //监听按钮事件
//    }



    private void initTableData() {
        // TODO Auto-generated method stub
        Postcode s = new Postcode("1", false, 1.1, 1.1, "yangfei", "男", "12", "a", "a");
        postcodeList.add(s);
        s = new Postcode("2", false, 1.2, 1.2, "yangfei", "男", "12", "b", "b");
        postcodeList.add(s);
        s = new Postcode("3", false, 1.3, 1.3, "yangfei", "男", "12", "c", "c");
        postcodeList.add(s);
    }




















    //Action事件处理
    class MyActionListener implements ActionListener
    {
//        @Override
        public void actionPerformed(ActionEvent arg0)
        {
            JFileChooser fc=new JFileChooser("F:\\");
            int val=fc.showOpenDialog(null);    //文件打开对话框
            if(val==fc.APPROVE_OPTION)
            {
                //正常选择文件
                jtf.setText(fc.getSelectedFile().toString());
            }
            else
            {
                //未正常选择文件，如选择取消按钮
                jtf.setText("未选择文件");
            }
        }
    }
    public static void main(String[] args)
    {
        Main main = new Main();
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setVisible(true);
    }

}
